/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Gson, parseString } from './index.js'

const jsonObj = {
  "date": "20211224",
  "news": [
    {
      "image_hue": "0xb39e7d",
      "title": "「东北大米」和「南方大米」有哪些区别？",
      "url": "https:\/\/daily.zhihu.com\/story\/9743658",
      "image": "https:\/\/pica.zhimg.com\/v2-fa83f855d3492183c4957bac445662bb.jpg?source=8673f162",
      "hint": "朴朴超市 · 1 分钟阅读",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743658",
      "thumbnail": "https:\/\/pic2.zhimg.com\/v2-a5ed468c9b6b115485c1b2295a061eb9.jpg?source=8673f162",
      "ga_prefix": "122407",
      "id": 9743658
    },
    {
      "image_hue": "0x7b5658",
      "title": "装置艺术和建筑学有什么关系？",
      "url": "https:\/\/daily.zhihu.com\/story\/9743655",
      "image": "https:\/\/pic2.zhimg.com\/v2-811786b1f919bb0979d9ad5b4f19a88e.jpg?source=8673f162",
      "hint": "知乎用户 · 2 分钟阅读",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743655",
      "thumbnail": "https:\/\/pic1.zhimg.com\/v2-0fe10dfb0f7e83c94d02f0278a605150.jpg?source=8673f162",
      "ga_prefix": "122407",
      "id": 9743655
    },
    {
      "image_hue": "0x202c2e",
      "title": "杠铃杆分为哪几种类型？不同类型所用的滚花也是不同的吗？",
      "url": "https:\/\/daily.zhihu.com\/story\/9743661",
      "image": "https:\/\/pic1.zhimg.com\/v2-4f432e109b20e20aba6e0f84003b0112.jpg?source=8673f162",
      "hint": "kmlover · 4 分钟阅读",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743661",
      "thumbnail": "https:\/\/pica.zhimg.com\/v2-4ab03c403d6eb0f3f0cfd350c3e80862.jpg?source=8673f162",
      "ga_prefix": "122407",
      "id": 9743661
    },
    {
      "image_hue": "0xb3957d",
      "title": "弹钢琴不都是按固定按键吗，为什么有技艺高低之分？",
      "url": "https:\/\/daily.zhihu.com\/story\/9743649",
      "image": "https:\/\/pic1.zhimg.com\/v2-2f6cd7299d04aecf35dd487b59bd6248.jpg?source=8673f162",
      "hint": "圭多达莱佐 · 1 分钟阅读",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743649",
      "thumbnail": "https:\/\/pica.zhimg.com\/v2-9020ca4fb321261a63ff915f67c66cb9.jpg?source=8673f162",
      "ga_prefix": "122407",
      "id": 9743649
    },
    {
      "image_hue": "0xb3867d",
      "title": "大误 · 用什么睡姿抱女友睡觉最舒服？",
      "url": "https:\/\/daily.zhihu.com\/story\/9743630",
      "image": "https:\/\/pica.zhimg.com\/v2-07b3126ba5a9e662825b77bf51cb8a09.jpg?source=8673f162",
      "hint": "丁香医生 · 2 分钟阅读",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743630",
      "thumbnail": "https:\/\/pic2.zhimg.com\/v2-98514a5075515728b31aa44b623ab66c.jpg?source=8673f162",
      "ga_prefix": "122407",
      "id": 9743630
    },
    {
      "image_hue": "0x413045",
      "title": "瞎扯 · 如何正确地吐槽",
      "url": "https:\/\/daily.zhihu.com\/story\/9743687",
      "image": "https:\/\/pic3.zhimg.com\/v2-d9b3363931476f6b9abab5ee55c9f30e.jpg?source=8673f162",
      "hint": "VOL.2818",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743687",
      "thumbnail": "https:\/\/pic1.zhimg.com\/v2-3f5cdf192c17cb4ab0160a56c48a56ad.jpg?source=8673f162",
      "ga_prefix": "122406",
      "id": 9743687
    }
  ],
  "is_today": true,
  "top_stories": [
    {
      "image_hue": "0x806e52",
      "image_source": "",
      "hint": "作者 \/ 博物",
      "url": "https:\/\/daily.zhihu.com\/story\/9743263",
      "image": "https:\/\/pic1.zhimg.com\/v2-1a619e3eea42084dad9ef118f6c72c87.jpg?source=8673f162",
      "title": "用臭鼬的臭臭臭鼬，臭鼬会被臭死吗？",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743263",
      "ga_prefix": "121507",
      "id": 9743263
    },
    {
      "image_hue": "0x363625",
      "image_source": "",
      "hint": "作者 \/ 了不起的苏小姐",
      "url": "https:\/\/daily.zhihu.com\/story\/9743204",
      "image": "https:\/\/pic3.zhimg.com\/v2-3b9ef2df261d1e28fbdd702bf10b2819.jpg?source=8673f162",
      "title": "有哪些隐含暗喻或细节、情节的名画？",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743204",
      "ga_prefix": "121207",
      "id": 9743204
    },
    {
      "image_hue": "0x2b323d",
      "image_source": "",
      "hint": "作者 \/ Yichen 在西海岸",
      "url": "https:\/\/daily.zhihu.com\/story\/9743303",
      "image": "https:\/\/pic3.zhimg.com\/v2-5128f83a1860091d3895663091e526b3.jpg?source=8673f162",
      "title": "摆拍和滤镜制造出来的美，是「假」吗？",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743303",
      "ga_prefix": "121407",
      "id": 9743303
    },
    {
      "image_hue": "0x785475",
      "image_source": "",
      "hint": "作者 \/ spRachel雷切爾",
      "url": "https:\/\/daily.zhihu.com\/story\/9743125",
      "image": "https:\/\/pic3.zhimg.com\/v2-d2974f26576b0c28afbe8140d77081ab.jpg?source=8673f162",
      "title": "哪些很出名的艺术作品容易让人感觉不舒服？",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743125",
      "ga_prefix": "121007",
      "id": 9743125
    },
    {
      "image_hue": "0x2d3b22",
      "image_source": "",
      "hint": "作者 \/ yang元祐",
      "url": "https:\/\/daily.zhihu.com\/story\/9743130",
      "image": "https:\/\/pic1.zhimg.com\/v2-e88ebc1504daeed20ea65d93abd15576.jpg?source=8673f162",
      "title": "为什么小种群更容易灭绝?",
      "share_url": "http:\/\/daily.zhihu.com\/story\/9743130",
      "ga_prefix": "120907",
      "id": 9743130
    }
  ],
  "display_date": "2021.12.24 星期五"
}

const gson = new Gson();

function fromJson_Test_01() {
  const jsonStr = gson.toJson(jsonObj)
  let result = null;
  let start = Date.now();
  for (let i = 0; i < 1000; i++) {
    result = gson.fromJson(jsonStr)
  }
  let end = Date.now();
  if (result.date != '20211224') {
    throw 'fromJson(string) error'
  }
  print(`Gson_fromJson(string): ${end - start} ms`);
}

function fromJson_Test_02() {
  const jsonStr = gson.toJson(jsonObj) 
  const element = parseString(jsonStr)
  let result = null;
  let start = Date.now();
  for (let i = 0; i < 1000; i++) {
    result = gson.fromJson(element)
  }
  let end = Date.now();
  if (result.date != '20211224') {
    throw 'fromJson(jsonElement) error'
  }
  print(`Gson_fromJson(jsonElement): ${end - start} ms`);
}




function toJson_Test_01() {
  let result = null;
  let start = Date.now();
  for (let i = 0; i < 1000; i++) {
    result = gson.toJson(jsonObj)
  }
  let end = Date.now();
  let obj = gson.fromJson(result)
  if (obj.date != '20211224') {
    throw 'toJson(object) error'
  }
  print(`Gson_toJson(object): ${end - start} ms`);
}

function toJson_Test_02() {
  const jsonStr = gson.toJson(jsonObj)
  const element = parseString(jsonStr)
  let result = null;
  let start = Date.now();
  for (let i = 0; i < 1000; i++) {
    result = gson.toJson(element)
  }
  let end = Date.now();
  if (result != jsonStr) {
    throw 'toJson(jsonElement) error'
  }
  print(`Gson_toJson(jsonElement): ${end - start} ms`);
}



function parseString_Test() {
  const jsonStr = gson.toJson(jsonObj)
  let result = null;
  let start = Date.now();
  for (let i = 0; i < 1000; i++) {
    result = parseString(jsonStr)
  }
  let end = Date.now();
  const size = result.get('news').size();
  if (size != jsonObj.news.length) {
    throw 'parseString error'
  }
  print(`Gson_parseString: ${end - start} ms`);
}


function toJsonTree_Test() {
  const jsonStr = gson.toJson(jsonObj)
  const json = gson.fromJson(jsonStr);
  let result = null;
  let start = Date.now();
  for (let i = 0; i < 1000; i++) {
    result = gson.toJsonTree(json)
  }
  let end = Date.now();
  const news = result.get('news')
  const url = news.elements[0].get('url').getAsString()

  if (url != jsonObj.news[0].url) {
    throw 'toJsonTree error'
  }
  print(`Gson_toJsonTree: ${end - start} ms`);
}




function gson_test() {
  fromJson_Test_01();
  fromJson_Test_02();
  toJson_Test_01();
  toJson_Test_02();
  parseString_Test();
  toJsonTree_Test();
}
gson_test();
